package lx.exalarm.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import lx.exalarm.R;
import lx.exalarm.alarm.AlarmBean;
import lx.exalarm.alarm.AlarmUtils;
import lx.exalarm.base.BaseActivity;
import lx.exalarm.base.BasePlayActivity;
import lx.exalarm.database.DBManager;
import lx.exalarm.utils.log.Log;
import lx.exalarm.view.WeekdayView;
import lx.exalarm.view.switchbutton.SwitchButton;


public class ActivityMain extends BaseActivity
        implements BaseActivity.ActionBarCallbacks,
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, View.OnClickListener {

    private ListView mListView;
    private AlarmAdapter mAdapter;
    private List<AlarmBean> mAlarmList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = getView(R.id.am_alarm_listview);
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
        getView(R.id.am_btn_add_alarm).setOnClickListener(this);
        getView(R.id.am_head_img).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mListView.setAdapter(initAdapter());
    }

    @Override
    public void onCreateActionBar(ImageView back, ImageView menu, TextView title) {
        back.setVisibility(View.INVISIBLE);
        menu.setVisibility(View.INVISIBLE);
    }

    @Override
    public boolean onActionBarBackClicked(View back) {
        return false;
    }

    @Override
    public boolean onActionBarMenuClicked(View menu) {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AlarmAdapter adapter = (AlarmAdapter) parent.getAdapter();
        final AlarmBean alarm = adapter.getItem(position);
        ActivityAlarm.start(this, alarm);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        AlarmAdapter adapter = (AlarmAdapter) parent.getAdapter();
        final AlarmBean alarm = adapter.getItem(position);
        AlertDialog dlg = new AlertDialog.Builder(this)
                .setMessage(R.string.dlg_delete_alarm_msg)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBManager.getInstance().deleteAlarm(alarm);
                        mAlarmList.remove(alarm);
                        mAdapter.dataChanged(mAlarmList);

                        // cancel deleted alarm
                        alarm.setEnabled(false);
                        AlarmUtils.updateAlarm(alarm);
                    }
                })
                .setNegativeButton(android.R.string.cancel, null)
                .create();
        dlg.show();
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.am_btn_add_alarm: {
                ActivityAlarm.start(ActivityMain.this);
                break;
            }
            case R.id.am_head_img: {
                // test code only
                BasePlayActivity.start(ActivityPlayVideo.class,
                        mAlarmList.get(0).getFirstEnabledAction());
                break;
            }
        }
    }

    private AlarmAdapter initAdapter() {
        mAlarmList = DBManager.getInstance().getAlarmList();
        Log.d("ActivityMain, init from database:");
        if (mAlarmList != null && mAlarmList.size() > 0) {
            for (AlarmBean alarm : mAlarmList) {
                Log.d(alarm.toString());
            }
        }
        mAdapter = new AlarmAdapter(this, mAlarmList);
        return mAdapter;
    }


    private static class AlarmAdapter extends BaseAdapter {

        private List<AlarmBean> mList;
        private Context mContext;

        AlarmAdapter(Context cxt, List<AlarmBean> list) {
            mContext = cxt;
            mList = list;
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public AlarmBean getItem(int position) {
            return mList == null ? null : mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final AlarmBean alarm = getItem(position);
            ViewHolder h;

            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                View view = inflater.inflate(R.layout.item_alarm, null);
                h = new ViewHolder();
                h.mTitle = (TextView) view.findViewById(R.id.item_alarm_time);
                h.mSwitch = (SwitchButton) view.findViewById(R.id.item_alarm_switch);
                h.mWeekday = (WeekdayView) view.findViewById(R.id.item_alarm_weekday);
                view.setTag(h);
                convertView = view;
            } else {
                h = (ViewHolder) convertView.getTag();
            }

            h.mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked && !alarm.isEnabled()) {
                        alarm.setEnabled(true);
                        DBManager.getInstance().updateAlarmEnabled(alarm);
                        AlarmUtils.updateAlarm(alarm);
                    } else if (!isChecked && alarm.isEnabled()) {
                        alarm.setEnabled(false);
                        DBManager.getInstance().updateAlarmEnabled(alarm);
                        AlarmUtils.updateAlarm(alarm);
                    }
                }
            });
            h.mTitle.setText(alarm.getTimeTitle());
            h.mWeekday.setAlarm(alarm, false);
            h.mSwitch.setChecked(alarm.isEnabled());

            return convertView;
        }

        public void dataChanged(List<AlarmBean> list) {
            mList = list;
            notifyDataSetChanged();
        }

        static class ViewHolder {
            TextView mTitle;
            SwitchButton mSwitch;
            WeekdayView mWeekday;
        }
    }

}
